package com.springcloud.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * TODO (websocker配置类，配置客户端向服务端发送消息)
 *
 * @author 肖嘉亮
 * @version 1.0
 * @date 2021/4/15 17:42
 */
@Configuration
@EnableWebSocket
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        //注册一个 /notification 端点 ， 前端通过端点连接
        registry.addEndpoint("/notification")
                //处理跨域
        .setAllowedOrigins("*").withSockJS();
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        //定义客户点订阅地址得到前缀信息，客户端接收服务端发送消息的前缀信息
        registry.enableSimpleBroker("/topic");
    }
}
